﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Qianwei.Common.Extension
{
    /// <summary>
    /// added by Martin 2011-1-15
    /// </summary>
    public static class PageExtension
    {
        public static void Alert(this Page page, string message)
        {
            string js = "<script>alert('" + message + "')</script>";
            page.ClientScript.RegisterStartupScript(page.GetType(), "message", js);
        }

        public static string Confirm(this Page page, string message)
        {
            return "javascript:var result = window.confirm('" + message + "'); if(!result) return false;";
        }

        public static void Clear(this Page page, ControlCollection controls)
        {

            if (controls.Count == 0) return;
            else
            {
                foreach (Control control in controls)
                {
                    if (control is TextBox)
                        (control as TextBox).Text = "";

                    if (control is DropDownList)
                        (control as DropDownList).SelectedIndex = -1;

                    if (control is CheckBox)
                        (control as CheckBox).Checked = false;

                    if (control is HiddenField)
                        (control as HiddenField).Value = "";

                    if (control is ListBox)
                        (control as ListBox).Items.Clear();

                    if (control is RadioButton)
                        (control as RadioButton).Checked = false;

                    Clear(page, control.Controls);
                }
            }
        }
    }
}
